//
//  GameLevelMainView.m
//  linkingGame
//
//  Created by zw wu on 15/4/11.
//  Copyright (c) 2015年 com.jinWu. All rights reserved.
//

#import "GameLevelMainView.h"
#import "GameLevelCellView.h"

#define KPageMaxCount 25

@interface GameLevelMainView ()<UIScrollViewDelegate>

@property (nonatomic,assign) int levelCount;
@property (nonatomic,assign) int unlockLevelCount;
@property (nonatomic,retain) UIPageControl *pageControl;

@end

@implementation GameLevelMainView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.delegate = self;
        
        UIPageControl *pageControl = [[UIPageControl alloc]init];
        self.pageControl = pageControl;
        [pageControl release];
    }
    
    return self;
}

- (void)dealloc
{
    self.gameDelegate = nil;
    self.pageControl = nil;
    
    [super dealloc];
}

- (void)resetLevelCount:(int)count
{
    if (count < KPageMaxCount)
    {
        return;
    }
    
    self.levelCount = count;
}

- (void)resetUnlockLevelCount:(int)count
{
    if (count < 0)
    {
        return;
    }
    
    self.unlockLevelCount = count;
    
    for (UIView *subView in self.subviews)
    {
        if ([subView isKindOfClass:[GameLevelCellView class]])
        {
            GameLevelCellView *view = (GameLevelCellView *)subView;
            if (view.levelTag <= self.unlockLevelCount)
            {
                [view resetUnlockState:YES];
            }
            else
            {
                [view resetUnlockState:NO];
            }
        }
    }
}

- (void)refreshUI
{
    int pageCount = (self.levelCount - 1) / KPageMaxCount + 1;
    self.contentSize = CGSizeMake(self.frame.size.width * pageCount, self.frame.size.height);
    
    self.pageControl.frame = CGRectMake(0, CGRectGetMaxY(self.frame) - 30, self.frame.size.width, 20);
    self.pageControl.numberOfPages = pageCount;
    [self.superview addSubview:self.pageControl];
    
    for (int i = 1; i <= self.levelCount; i++)
    {
        int pageNum = (i - 1) / KPageMaxCount;
        int cellNumInPage = (i % KPageMaxCount == 0)?KPageMaxCount:(i % KPageMaxCount);
        int cell_x = (cellNumInPage % 5 + 4) % 5;
        int cell_y = (cellNumInPage - 1) / 5;
        int cellSize = 100;
        if (isPhone())
        {
            cellSize = 50;
        }
        
        int between_x = (self.bounds.size.width - 5 * cellSize) / 6;
        int between_y = (self.bounds.size.height - 5 * cellSize - 20) / 6;
        
        CGRect cellRect = CGRectMake(cell_x * cellSize + (cell_x + 1) * between_x + pageNum * self.bounds.size.width, cell_y * cellSize + (cell_y + 1) * between_y, cellSize, cellSize);
        
        UIView *cellViewTmp = [self viewWithTag:(1000 + i)];
        if (cellViewTmp && cellViewTmp.superview)
        {
            cellViewTmp.frame = cellRect;
        }
        else
        {
            GameLevelCellView *cellView = [[GameLevelCellView alloc]initWithFrame:cellRect];
            [cellView.levelBtn addTarget:self action:@selector(startGameBtnChecked:) forControlEvents:UIControlEventTouchUpInside];
            cellView.backgroundColor = [UIColor whiteColor];
            cellView.levelTag = i;
            cellView.tag = 1000 + i;
            [self addSubview:cellView];
            [cellView release];
            
            [cellView resetStars:i];
        }
    }
}

- (void)startGameBtnChecked:(UIButton *)sender
{
    GameLevelCellView *cellView = (GameLevelCellView *)sender.superview;
    if ([self.gameDelegate respondsToSelector:@selector(startGameAtLevel:)])
    {
        [self.gameDelegate startGameAtLevel:cellView.levelTag];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float center_x = scrollView.contentOffset.x + scrollView.frame.size.width / 2 - 1;
    int currentPageNum = (int)(center_x / scrollView.frame.size.width);
    self.pageControl.currentPage = currentPageNum;
}

@end
